import Vue from 'vue'
import VueRouter from 'vue-router'

const Home = () => import(/*webpackChunkName: "home_home"*/ '../page/Home.vue')
const Search = () => import(/*webpackChunkName: "home_home"*/ '../page/Search.vue')
const Topic = () => import(/*webpackChunkName: "other_other"*/ '../page/Topic.vue')
const Category = () => import(/*webpackChunkName: "other_other"*/ '../page/Category.vue')
const Cart = () => import(/*webpackChunkName: "home_home"*/ '../page/Cart.vue')
const User = () => import(/*webpackChunkName: "other_other"*/ '../page/User.vue')
const Channel = () => import(/*webpackChunkName: "home_home"*/ '../page/Channel.vue')
const Brand = () => import(/*webpackChunkName: "home_home"*/ '../page/Brand.vue')
const SearchDetails = () => import(/*webpackChunkName: "home_home"*/ '../page/SearchDetails.vue')

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    // 解决重复进入同一个路由问题
    return originalPush.call(this, location).catch(err => err)
}
const originalReplace = VueRouter.prototype.replace
VueRouter.prototype.replace = function push(location) {
    // 解决重复进入同一个路由问题
    return originalReplace.call(this, location).catch(err => err)
}
const router = new VueRouter({
    routes: [
        {
            path: '/',
            redirect: '/home'
        },
        {
            path: '/home',
            name: 'home',
            component: Home,
            meta: {
                bar: true
            },
            children: [
                {
                    path: 'search',
                    name: 'search',
                    component: Search
                }
            ]
        },
        {
            path: '/topic',
            name: 'topic',
            component: Topic,
            meta: {
                bar: true
            }
        },
        {
            path: '/category',
            name: 'category',
            meta: {
                bar: true
            },
            component: Category
        },
        {
            path: '/cart',
            name: 'cart',
            meta: {
                bar: true
            },
            component: Cart
        },
        {
            path: '/user',
            name: 'user',
            meta: {
                bar: true
            },
            component: User
        },
        {
            path: '/channel',
            name: 'channel',
            meta: {
                bar: true
            },
            component: Channel
        },
        {
            path: '/brand',
            name: 'brand',
            meta: {
                bar: true
            },
            component: Brand
        },
        {
            path: '/SearchDetails',
            name: 'SearchDetails',
            meta: {
                bar: false
            },
            component: SearchDetails
        }
    ]
})
router.beforeEach((to, from, next) => {
    if (to.path !== '/cart') {
        next()
        return
    }
    let token = sessionStorage.getItem('token')
    if (!token) {
        Vue.prototype.$toast.fail({
            duration: 800,
            message: '请先登录'
        })
        next({
            name: 'user'
        })
        return
    }
    next()
})
export default router
